package com.ibt.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ibt.entity.BuildIds;
import com.ibt.entity.Employee;
import com.ibt.entity.Module;
import com.ibt.util.ApplicationContext;

public class BuildIdsDAO {
	static Transaction tx;

	public static int insert(BuildIds b) throws Exception {

		Session session = startOperations();
		session.saveOrUpdate(b);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
	}

	public static int update(BuildIds b) throws Exception {
		Session session = startOperations();
		Query qry = session
				.createQuery("update BuildIds set current_points=:points where business_id=:busid and customer_id=:custid");
		//qry.setInteger("points", b.getCurrentPoints());
		////qry.setInteger("busid", b.getBusiness().getBusinessId());
		//qry.setInteger("custid", b.getCustomer().getCustomerId());
		Object ob = qry.executeUpdate();
		session.getTransaction().commit();
		endOperations(session);
		return 0;
	}

	public static int delete(BuildIds b) throws Exception {
		Session session = startOperations();
		session.delete(b);
		// tx.commit();
		endOperations(session);
		return 0;
	}

	private static void endOperations(Session session) {
		session.close();
		session = null;
	}

	private static Session startOperations() throws Exception {
		Session session = ApplicationContext.getHiberSession().openSession();
		tx = session.beginTransaction();
		return session;
	}

	public static List<BuildIds> findAll() throws Exception {
		List<BuildIds> custObjs = new ArrayList<BuildIds>();
		List objs = null;
		Session session = startOperations();
		Query query = session.createQuery("from BuildIds");
		objs = query.list();
		for (Object o : objs) {
			custObjs.add((BuildIds) o);
		}
		endOperations(session);
		return custObjs;
	}

	public static BuildIds findById(int i) throws Exception {
		Session session = startOperations();
		BuildIds c = (BuildIds) session.load(
				BuildIds.class, i);
		endOperations(session);
		return c;
	}

	
	public static List<BuildIds> getBuildList(int projId,int modId ,int busid)
	throws Exception {
		
Session session = startOperations();
Query qry = session.createQuery("from BuildIds where project_id=:projid and business_id=:busid and module_id=:modid");
qry.setInteger("projid", projId);
qry.setInteger("modid", modId);
qry.setInteger("busid", busid);
List<Object> obj = qry.list();
ArrayList<BuildIds> lst = new ArrayList<BuildIds>();
for (Object o : obj) {
	System.out.println("obj is " + obj);
	lst.add((BuildIds) o);
}
endOperations(session);
return lst;

}
	public static Object getByCustBusId(int id, int i) throws Exception {
		Session session = startOperations();
		System.out.println("id in dao " + id);
		Query qry = session
				.createQuery("from BuildIds where customer_id=:id and business_id=:bid");
		qry.setInteger("id", id);
		qry.setInteger("bid", i);
		Object obj = qry.uniqueResult();
		System.out.println("obj is " + obj);
		endOperations(session);
		return obj;

	}
	public static Employee getUserByBuildId(int projId,int modId ,int busid,String buildid) throws Exception {
		Session session = startOperations();
		//Select * from employee where empname=(select developer_name from buildids where project_id=1 and business_id=1 and module_id=1 and build_name="akhilbuild")
		Query qry = session.createSQLQuery("select * from  employee e where e.empname=(select developer_name from buildids b where b.project_id="+projId+"  and b.business_id="+busid+"  and b.module_id="+modId+" and b.build_name='"+buildid+"')").addEntity(Employee.class);;
		Employee emp=(Employee)qry.uniqueResult();;
		System.out.println("obj is " + emp);
		endOperations(session);
		return emp;

	}
	public static BuildIds getUserByBuildName(String buildid,int projid,int modId) throws Exception {
		Session session = startOperations();
		Query qry = session.createQuery("from BuildIds where  module_id=:modid and project_id=:projid and build_name=:buildid");
		qry.setInteger("modid", modId);
		qry.setInteger("projid", projid);
		qry.setString("buildid",buildid);
		Object obj = qry.uniqueResult();
		BuildIds build=(BuildIds)obj;
		System.out.println("obj is " + obj);
		endOperations(session);
		return build;

	}
	public static List<BuildIds> getBusinessCustMap(int userid)
			throws Exception {
		Session session = startOperations();
		System.out.println("id in dao " + userid);
		Query qry = session
				.createQuery("from BuildIds where customer_id=:userid");
		qry.setInteger("userid", userid);
		List<Object> obj = qry.list();
		ArrayList<BuildIds> lst = new ArrayList<BuildIds>();
		for (Object o : obj) {
			System.out.println("obj is " + obj);
			lst.add((BuildIds) o);
		}
		endOperations(session);
		return lst;

	}

	public static List<BuildIds> findByBusId(int bsid) throws Exception {

		Session session = startOperations();
		Query qry = session
				.createQuery("from BuildIds where business_id=:id");
		qry.setInteger("id", bsid);
		List<BuildIds> listBC = qry.list();
		endOperations(session);
		return listBC;
	}

}
